﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Directory.Web
{
    public partial class Admin_Controls_PageBreak : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            SetPageNav();
            base.Render(writer);
        }

        public int TotalRecords = 0;
        public int PageSize = 10;
        public int PageIndex = 0;
        public string PageKey = "page";
        public string NavigatorUrl = "";

        public string GetCurrentPageUrl()
        {
            return Server.UrlEncode(this.GetPageNavigateUrl(this.PageIndex));
        }

        public void SetPageNav()
        {
            int pageCounts = ((TotalRecords - 1) / PageSize) + 1;
            lbCurrentPage.Text = (PageIndex + 1) + " / " + pageCounts;

            if (pageCounts < 1)
            {
                hplFirstPage.Enabled = false;
                hplPrePage.Enabled = false;
                hplNextPage.Enabled = false;
                hplLastPage.Enabled = false;
            }
            else if (PageIndex == 0)
            {
                hplFirstPage.Enabled = false;
                hplPrePage.Enabled = false;
                if (pageCounts == 1)
                {
                    hplNextPage.Enabled = false;
                    hplLastPage.Enabled = false;
                }
                else
                {
                    hplNextPage.Enabled = true;
                    hplNextPage.NavigateUrl = GetPageNavigateUrl(PageIndex + 1);
                    hplLastPage.Enabled = true;
                    hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts - 1);
                }
            }
            else if (PageIndex >= pageCounts - 1)
            {
                PageIndex = pageCounts - 1;
                hplFirstPage.Enabled = true;
                hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
                hplPrePage.Enabled = true;
                hplPrePage.NavigateUrl = GetPageNavigateUrl(PageIndex - 1);
                hplNextPage.Enabled = false;
                hplLastPage.Enabled = false;
            }
            else
            {
                hplFirstPage.Enabled = true;
                hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
                hplPrePage.Enabled = true;
                hplPrePage.NavigateUrl = GetPageNavigateUrl(PageIndex - 1);
                hplNextPage.Enabled = true;
                hplNextPage.NavigateUrl = GetPageNavigateUrl(PageIndex + 1);
                hplLastPage.Enabled = true;
                hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts - 1);
            }
        }

        private string GetPageNavigateUrl(int index)
        {
            return NavigatorUrl + PageKey + "=" + index.ToString();
        }
    }
}
